package com.link.base.task;

import com.link.base.base.appduty.model.AppDuty;
import com.link.base.base.appduty.service.AppDutyService;
import com.link.base.base.duty.model.Duty;
import com.link.base.base.duty.service.DutyService;
import com.link.base.base.dutyallurls.service.DutyAllUrlsService;
import com.link.base.core.basic.config.xxljob.BaseLinkJobHandler;
import com.link.core.util.DateUtil;
import com.link.core.util.RedisUtil;
import com.link.core.util.StringUtils;
import com.link.core.util.redisclient.LinkRedisClient;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * 职责权限变动定时任务
 *
 * @author qinxuegang
 * @date 2019-03-08
 */
@JobHandler(value = "dutyChangeJobHandler")
@Component
public class TaskDutyChange extends BaseLinkJobHandler {

    private final String TIME_KEY = "DUTY_TASK_TIME_KEY";

    @Resource
    private DutyAllUrlsService dutyAllUrlsService;

    @Resource
    private DutyService dutyService;
    @Resource
    private AppDutyService appDutyService;


    @Override
    public ReturnT<String> execute(String params) throws Exception {
        LinkRedisClient jredis = null;
        try {
            jredis = RedisUtil.getJedis();
            XxlJobLogger.log("职责权限变种更新权限开始......");
            XxlJobLogger.log("职责权限变种更新权限开始时间:[ " + DateUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss") + " ]");
            super.setLogin(params);
            // 获取CRM/DMS变动的职责
            Map<String, String> dutyInfo = jredis.hgetAll("LINK_OBJ_CHANGE_CRM_DMS_DUTY");
            String time = jredis.get(TIME_KEY);
            Long currentTime = System.currentTimeMillis();
//            List<String> time = dutyAllUrlsService.getLatsDutyChangeTime();
            // 第一次执行的时候
            if (StringUtils.isBlank(time)) {
                for (String dutyId : dutyInfo.keySet()) {
                    Duty duty = new Duty();
                    duty.setId(Long.valueOf(dutyId));
                    dutyService.refreshPermByDutyId(duty);
                }
            } else {
                Long timeLong = Long.valueOf(time);
                for (String dutyId : dutyInfo.keySet()) {
                    if (Long.valueOf(dutyInfo.get(dutyId)) > timeLong) {
                        Duty duty = new Duty();
                        duty.setId(Long.valueOf(dutyId));
                        dutyService.refreshPermByDutyId(duty);
                    }
                }
            }

            // 获取APP变动的职责
            Map<String, String> appDutyInfo = jredis.hgetAll("LINK_OBJ_CHANGE_APP_DUTY");
            // 第一次执行的时候
            if (StringUtils.isBlank(time)) {
                for (String dutyId : appDutyInfo.keySet()) {
                    AppDuty duty = new AppDuty();
                    duty.setId(Long.valueOf(dutyId));
                    appDutyService.refreshPermByDutyId(duty);
                }
            } else {
                Long timeLong = Long.valueOf(time);
                for (String dutyId : appDutyInfo.keySet()) {
                    if (Long.valueOf(appDutyInfo.get(dutyId)) > timeLong) {
                        AppDuty duty = new AppDuty();
                        duty.setId(Long.valueOf(dutyId));
                        appDutyService.refreshPermByDutyId(duty);
                    }
                }
            }
            jredis.set(TIME_KEY, currentTime + "");
            XxlJobLogger.log("职责权限变种更新权限结束时间:[ " + DateUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss") + " ]");
            return ReturnT.SUCCESS;
        } catch (Exception e) {
            XxlJobLogger.log(e.getMessage());
            return ReturnT.FAIL;
        } finally {
            RedisUtil.returnResource(jredis);
        }
    }

}

